home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / CLOSEMOD / CLOSEMOD.C
C/C++ Source or Header  |  1991-02-20  |  7KB  |  196 lines

  1. /*    Mon, Feb 13, 1989, 2:35:51 PM    *******************************************
  2. *******************************************************************************
  3. **
  4. **        A sample to demonstrate use of the RAM Serial driver functions
  5. **        by a trivial program.
  6. **
  7. **        by:    Mario P. Vano, VAI
  8. **
  9. **        Copyright ⌐1989 by:
  10. **
  11. **                Vano Associates, Incorporated
  12. **                2513 Pahl Ave NE
  13. **                St. Anthony, MN        55418
  14. **                (612)-788-9547
  15. **
  16. **        (Creators of The MacChuck(tm) PC Remote Control Program)
  17. **
  18. **        Permission is hereby unconditionally granted to anyone to
  19. **        use this program fragment for ANY purpose whatsoever!
  20. **
  21. **        Note that this code does not represent a realistic way to make
  22. **        a terminal program, since it uses a very slow console IO method,
  23. **        and does very little error checking. It is only intended to show
  24. **        beginning Macintosh COMM programmers how to use the functions.
  25. **
  26. **        The Think C project needs to include
  27. **
  28. **            1)    SerialIO.C        - this source file
  29. **            2)    MacTraps
  30. **            3)    stdio
  31. **
  32. **        The Serial Resource file "SERD" delivered with your compiler needs
  33. **        to be present in the same folder as this application or project to run.
  34. **        A real application would of course contain the SERD resources in
  35. **        its own resource fork (merged by using ResEdit or RMaker.)
  36. **
  37. **    Revised Mon, Feb 13, 1989, 2:36:13 PM
  38. **        to show how to use SerSta function to read HSKi line.
  39. **
  40. *******************************************************************************
  41. ******************************************************************************/
  42. /*|*/
  43. /*| Mike Liveright -- 91/02/20
  44. /*|    Slightly Modified because Iall I want to do is to hang up the port, 
  45. /*| i.e. CLose it.
  46. /*|*/
  47. #include <Stdio.h>
  48. #include <SerialDvr.H>
  49. /*    Assumes the THINK C 3.0 "MacHeaders" include files in use    */
  50.  
  51.  
  52. main()
  53. {
  54. char rawBuffer[1024];            /* used by the DRIVER - we don't touch it    */
  55. char incoming[128];                /* our input buffer                            */
  56. short inputRef, outputRef;        /*    the RefNums                                */
  57. char outChar;                    /* holds outbound char gotten from keyboard    */
  58. SerShk cp;                        /* Serial Handshake mode setup record        */
  59. SerStaRec theStat;                /* Status record for reading port status    */
  60. long count, i;
  61. OSErr result;
  62.     
  63.     /*
  64.     *    To simplify debugging, let's try to open a copy of the resource file
  65.     *    called SERD that is supplied with LSC. If we put a copy of this file
  66.     *    in the same folder as the project, we can avoid building a resource
  67.     *    file while we are debugging. A real App would, of course, have the
  68.     *    drivers found in SERD in its resource file.
  69.     */
  70.     result = OpenResFile("\pSERD");                /* ignore any errors here    */
  71.  
  72.     inputRef  = AinRefNum;                /*    these are "well-known" refNums    */
  73.     outputRef = AoutRefNum;                /*    defined in SerialDrvr.h            */
  74.  
  75.     if ((result=RAMSDOpen(sPortA)) != noErr)
  76.             ;
  77. /*|*/
  78. /*|            printf("\nUnable to open modem port!\n"); /*Needed ANSII*/
  79. /*|*/
  80.     else
  81.         {
  82. /*|                printf("\n Terminal IO sample\n"); /*Needed ANSII*/
  83. /*|*/
  84. /*|                    
  85. /*|            |*
  86. /*|            *    Here's how to configure handshake options if you need to
  87. /*|            *|
  88. /*|            cp.errs = 0x70;                    |* build a SerShk record            *|
  89. /*|            cp.evts = 0;
  90. /*|            cp.fCTS = 0;                    |*    Disable CTS handshake for now    *|
  91. /*|            cp.fDTR = 0;                    |*    Disable DTR handshake for now    *|
  92. /*|            cp.fInX = 0;                    |*    We don't use input Xon-Xoff        *|
  93. /*|            cp.fXOn = 0;                    |*    Disable output Xon/Off for now    *|
  94. /*|            cp.xOn  = 0x11;                    |*    Ctrl-Q    would be handshake XON    *|
  95. /*|            cp.xOff = 0x13;                    |*    Ctrl-S    would be handshake XOFF    *|
  96. /*|            SerHShake(outputRef,&cp);        |*    really should check for errors     *|
  97. /*|            SerHShake(inputRef, &cp);
  98. /*|    
  99. /*|            |*
  100. /*|            *    Here's how to set baud, data bits and stop bits using constants
  101. /*|            *    defined in SerialDrvr.h
  102. /*|            *|
  103. /*|            SerReset(outputRef,data8 + stop10 + noParity + baud2400);
  104. /*|            SerReset(inputRef, data8 + stop10 + noParity + baud2400);
  105. /*|    
  106. /*|            |*
  107. /*|            *    Here's how to increase the driver's private input buffer size.
  108. /*|            *|
  109. /*|            SerSetBuf(inputRef, rawBuffer, sizeof(rawBuffer));
  110. /*|    
  111. /*|            |*********************************************************************
  112. /*|            *    loop here doing terminal emulation until the "Enter" key is hit
  113. /*|            *    (This is of course a terrible way to do this, but it will do for
  114. /*|            *    a sample of how to use the drivers).
  115. /*|            *********************************************************************|
  116. /*|            for (outChar = 0; outChar != 3;)    |* 3 == code of "Enter" key        *|
  117. /*|                {
  118. /*|                
  119. /*|                SerGetBuf(inputRef,&count);                |* any incoming bytes?    *|
  120. /*|                while (count)                            |* yes, display them     *|
  121. /*|                    {
  122. /*|                    if (count > sizeof(incoming)) count = sizeof(incoming);
  123. /*|                    if (    (FSRead(inputRef, &count, incoming)==noErr)
  124. /*|                        &&    count
  125. /*|                       )
  126. /*|                        for (i=0; i < count; i++)        |* Dumb way to do this!    *|
  127. /*|                            {
  128. /*|                            incoming[i] &= 0x7f;
  129. /*|                            if (incoming[i] !='\n')
  130. /*|                                {                        |* kludgy unix '\n' fix    *|
  131. /*|                                if (incoming[i]=='\r') incoming[i]='\n';
  132. /*|                                fputc(incoming[i],stdout);
  133. /*|                                }
  134. /*|                            }
  135. /*|                    SerGetBuf(inputRef,&count);            |*    Check if any more    *|
  136. /*|                    }
  137. /*|                    
  138. /*|                if (kbhit())                            |* Any outbound byte?    *|
  139. /*|                    {
  140. /*|                    outChar = getch() & 0x7F;            |*    get from keyboard    *|
  141. /*|                    if (outChar==('@' & 0x1f))            |* cmd @ displays stat *|
  142. /*|                        {
  143. /*|                        |*
  144. /*|                        *    This code is entered if you type Command-@.
  145. /*|                        *    It shows how to read the port status from the driver.
  146. /*|                        *    You can read the state of the HSKI handshake line here
  147. /*|                        *    by examining the byte "theStat.ctsHold".
  148. /*|                        *    If your modem is wired as per Apple cable specs, this
  149. /*|                        *    will be wired to its Data Carrier Detect line.
  150. /*|                        *    (Note that your modem configuration RAM or DIP
  151. /*|                        *    switches often override this use, however!)
  152. /*|                        *|
  153. /*|                        SerStatus(outputRef,&theStat);
  154. /*|                        printf("\nResults of SerStatus function call:");
  155. /*|                        printf("\n    cumErrs = %4d    xOffSent = %s    rdPend   = %s",
  156. /*|                                (short) theStat.cumErrs,
  157. /*|                                theStat.xOffSent    ? "TRUE " : "FALSE",
  158. /*|                                theStat.rdPend        ? "TRUE " : "FALSE"
  159. /*|                                );
  160. /*|                        printf("\n    wrPend  = %s   ctsHold  = %s    xOffHold = %s\n\n",
  161. /*|                                theStat.wrPend        ? "TRUE " : "FALSE",
  162. /*|                                theStat.ctsHold        ? "TRUE " : "FALSE",
  163. /*|                                theStat.xOffHold    ? "TRUE " : "FALSE"
  164. /*|                            );
  165. /*|                        }
  166. /*|                    else
  167. /*|                        {
  168. /*|                        if (outChar=='\n') outChar = '\r';    |*    do the Unix shuffle    *|
  169. /*|                        count = 1;                            |*    send it to the port    *|
  170. /*|                        FSWrite(outputRef, &count, &outChar);
  171. /*|                        }
  172. /*|                    }
  173. /*|                }
  174. /*|    
  175. /*|    
  176. /*|            |*
  177. /*|            *    Here's how to restore the default input buffer for driver.
  178. /*|            *    
  179. /*|            *    Warning! VERY BAD THINGS CAN HAPPEN (Perhaps even loss of data
  180. /*|            *    on your hard disk!) long after your program quits if you forget
  181. /*|            *    to do this, or forget to close the driver properly!
  182. /*|            *    
  183. /*|            *    It is essential that you do not ever leave a custom input buffer
  184. /*|            *    dangling, EVEN if you abort your program for some other reason!
  185. /*|            *    The OS will NOT fix this up for you! You have been warned!
  186. /*|            *|
  187. /*|            SerSetBuf(inputRef,rawBuffer,0); |* how to restore default buffer    *|
  188. /*|    
  189. /*|*/
  190.         /*
  191.         *    Here's how to close the driver
  192.         */
  193.         RAMSDClose(sPortA);
  194.  
  195.         }
  196. }